/**
 * @ClassName BioRemovalReportServiceImpl
 * @Description TODO
 * @Author 沉默小吴
 * @Date 2024/12/19 9:22
 */
package com.tledu.operate.count.service.impl;

import com.tledu.operate.count.entity.BioRemovalReport;
import com.tledu.operate.count.entity.vo.BioRemovalReportJSON;
import com.tledu.operate.count.mapper.BioRemovalReportRepository;
import com.tledu.operate.count.service.IBioRemovalReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

@Service
public class BioRemovalReportServiceImpl implements IBioRemovalReportService {

    @Autowired
    private BioRemovalReportRepository bioRemovalReportRepository;

    @Override
    public List<BioRemovalReportJSON> getBioRemovalReportByDateRange(Date startDate, Date endDate) {
        // 将 Date 转换为 LocalDate
        LocalDate startLocalDate = convertToDate(startDate).plusDays(1);
        LocalDate endLocalDate = convertToDate(endDate).plusDays(1);

        // 初始化日期时间
        LocalDateTime startDateTime = startLocalDate.atStartOfDay(); // 设置为0时0分0秒
        LocalDateTime endDateTime = endLocalDate.atTime(23, 59, 59, 999999999); // 设置为23时59分59秒

        // 上周起始时间
        LocalDate startOfWeek = startLocalDate.minusDays(7);
        LocalDateTime startOfWeekDateTime = startOfWeek.atStartOfDay();

        // 上周结束时间
        LocalDate endOfWeek = endLocalDate.minusDays(7);
        LocalDateTime endOfWeekDateTime = endOfWeek.atTime(23, 59, 59, 999999999);

        // 将 LocalDateTime 转换为 Date
        Date startDateTimeAsDate = convertToDate(startDateTime);
        Date endDateTimeAsDate = convertToDate(endDateTime);
        Date startOfWeekDateTimeAsDate = convertToDate(startOfWeekDateTime);
        Date endOfWeekDateTimeAsDate = convertToDate(endOfWeekDateTime);

        // 查询本周的数据
        List<BioRemovalReport> bioRemovalReports = bioRemovalReportRepository.getBioRemovalReport(startDateTimeAsDate, endDateTimeAsDate);

        // 查询上周的数据
        List<BioRemovalReport> bioRemovalReportsBefore7Days = bioRemovalReportRepository.getBioRemovalReport(startOfWeekDateTimeAsDate, endOfWeekDateTimeAsDate,"这是上一周的数据,做区分");

        // 计算去除率
        List<BioRemovalReportJSON> bioRemovalReportJSONS = new ArrayList<>();
        for (BioRemovalReport bioRemovalReportsThisWeek : bioRemovalReports) {
            for (BioRemovalReport bioRemovalReportsBeforeWeek : bioRemovalReportsBefore7Days) {
                if (bioRemovalReportsThisWeek.getProject().equals(bioRemovalReportsBeforeWeek.getProject())) {
                    BioRemovalReportJSON bioRemovalReportJSON = new BioRemovalReportJSON();
                    // 部门
                    bioRemovalReportJSON.setProject(bioRemovalReportsThisWeek.getProject());
                    // 上周调节池均值
                    bioRemovalReportJSON.setRegulationPoolAvg1(bioRemovalReportsBeforeWeek.getRegulatingPoolAvg());
                    // 上周二沉池均值
                    bioRemovalReportJSON.setSecondarySedimentationAvg1(bioRemovalReportsBeforeWeek.getSSTAvg());
                    // 上周生化去除率
                    bioRemovalReportJSON.setBioRemovalRate1(calculateBioRemovalRate(bioRemovalReportsBeforeWeek.getRegulatingPoolAvg(), bioRemovalReportsBeforeWeek.getSSTAvg()));
                    // 本周调节池均值
                    bioRemovalReportJSON.setRegulationPoolAvg2(bioRemovalReportsThisWeek.getRegulatingPoolAvg());
                    // 本周二沉池均值
                    bioRemovalReportJSON.setSecondarySedimentationAvg2(bioRemovalReportsThisWeek.getSSTAvg());
                    // 本周生化去除率
                    bioRemovalReportJSON.setBioRemovalRate2(calculateBioRemovalRate(bioRemovalReportsThisWeek.getRegulatingPoolAvg(), bioRemovalReportsThisWeek.getSSTAvg()));
                    // 调节池均值差值
                    bioRemovalReportJSON.setRegulationPoolDiff(bioRemovalReportsThisWeek.getRegulatingPoolAvg().subtract(bioRemovalReportsBeforeWeek.getRegulatingPoolAvg()));
                    // 二沉池均值差异
                    bioRemovalReportJSON.setSecondarySedimentationDiff(bioRemovalReportsThisWeek.getSSTAvg().subtract(bioRemovalReportsBeforeWeek.getSSTAvg()));
                    // 去除率差异
                    bioRemovalReportJSON.setBioRemovalRateDiff(bioRemovalReportJSON.getBioRemovalRate2().subtract(bioRemovalReportJSON.getBioRemovalRate1()));
                    bioRemovalReportJSONS.add(bioRemovalReportJSON);
                }
            }
        }
        return bioRemovalReportJSONS;
    }

    private LocalDate convertToDate(Date date) {
        return date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
    }

    private Date convertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    private BigDecimal calculateBioRemovalRate(BigDecimal regulatingPoolAvg, BigDecimal SSTAvg) {
        if (regulatingPoolAvg.compareTo(BigDecimal.ZERO) == 0) {
            return BigDecimal.ZERO;
        }
        return regulatingPoolAvg.subtract(SSTAvg)
                .divide(regulatingPoolAvg, 4, RoundingMode.HALF_UP) // 保留四位小数，四舍五入
                .multiply(BigDecimal.valueOf(100));
    }
}